package leecode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cloudJiang
 * @date 2021/10/22 10:01
 */
public class Num229 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};

        System.out.println(majorityElement(nums));
    }

    public static List<Integer> majorityElement(int[] nums) {
        ArrayList<Integer> numCount = new ArrayList<>();
        ArrayList<Integer> num = new ArrayList<>();
        ArrayList<Integer> finalNum = new ArrayList<>();
        float length = nums.length / 3;

        //遍历nums
        for (int i : nums) {
            if (num.contains(i)) {
                //获得array list该元素的下标
                int index = num.indexOf(i);
                //count 加一
                numCount.set(index, numCount.get(index) + 1);
            } else {
                num.add(i);
                numCount.add(1);
            }
        }
        //遍历numCount，若值大于length则加入到finalNum
        int index = 0;
        for (int i : numCount) {
            if (i > length) {
                finalNum.add(num.get(index));
            }
            index++;
        }
        return finalNum;
    }
}
